﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using DataLayer;
using DataLayer.Generic;
namespace Tests
{
    [TestFixture]
    public class TestObjectUpdater
    {
        ObjectUpdater _objectUpdater = new ObjectUpdater(new NorthwindDataContext());
        LinqMappingHelper _mappingHelper = new LinqMappingHelper(new NorthwindDataContext().Mapping);
        Category c1;
        Category c2;
        [TestFixtureSetUp]
        public void SetUp()
        {
            c1 = new Category
            {
                CategoryID = 1,
                Description = "Test Category",
                CategoryName = "Test Category",
                Products = new System.Data.Linq.EntitySet<Product>()
            };

            c1.Products.Add(new Product
            {
                ProductID = 1,
                ProductName = "Test product"
            });

            c1.Products.Add(new Product
            {
                ProductID = 2,
                ProductName = "Test product 2"
            });

            c2 = new Category();
            c2.Products.Add(new Product
            {
                ProductID = 3,
                ProductName = "Test product 3"
            });
        }
        [Test]
        public void test_update_no_children()
        {
            Product p1 = new Product { ProductID = 1, CategoryID = 2, ProductName = "Test product" };
            Product p2 = new Product();
            _objectUpdater.Update(p1, p2, false);
            Assert.That(p1.ProductID == p2.ProductID && p1.CategoryID == p2.CategoryID & p1.ProductName == p2.ProductName);
        }
        
        
        [Test]
        public void test_update_with_children()
        {
            _objectUpdater.Update(c1, c2, true);

            Assert.That(c1.CategoryName == c2.CategoryName && c1.CategoryID == c2.CategoryID, "Top level comparison failed");
            Assert.That(c1.Products.Count == c2.Products.Count &&
                c1.Products[0].ProductID == c2.Products[0].ProductID &&
                c1.Products[0].CategoryID == c2.Products[0].CategoryID &&
                c1.Products[0].ProductName == c2.Products[0].ProductName, "Comparison of children failed");
        }
        [Test]
        public void test_update_with_children_where_children_is_empty()
        {
            c1 = c1 = new Category
            {
                CategoryID = 1,
                Description = "Test Category",
                CategoryName = "Test Category",
                Products = new System.Data.Linq.EntitySet<Product>()
            };
            _objectUpdater.Update(c1, c2, true);

            Assert.That(c1.CategoryName == c2.CategoryName && c1.CategoryID == c2.CategoryID, "Top level comparison failed");
            Assert.That(c1.Products.Count == 0, "Comparison of children failed");
        }
        [Test]
        public void test_UpdateChildren()
        {
            EntitySet<Product> p = c2.Products;
            _objectUpdater.UpdateChildCollection<Product>(c1.Products, p);
        }
        [Test]
        public void test_getAssociations()
        {
            
            var props = _mappingHelper.GetAssociationProperties(typeof(Category));
            //category should have 1 association of the type we're interested in, to the Product table
            Assert.That(props.Count == 1 && props[0].Name == "Products");

            props = _mappingHelper.GetAssociationProperties(typeof(Order));
            //Order should have 1 association of the type we're interested in, to the Order_Detail table, it also has a number of
            //other associations that should not be retrieved
            Assert.That(props.Count == 1 && props[0].Name == "Order_Details");

            props = _mappingHelper.GetAssociationProperties(typeof(Employee));
            //Order should have 3 associations of the type we're interested in
            Assert.That(props.Count == 3);

        }
    }
}
